package com.jia.leetCode;

/**
 * @program: Leetcode
 * @description: Remove Duplicates from Sorted List
 * @author: STU756
 * @create: 2020-08-17 18:44
 */
public class Pro0083 {
    //迭代
    public ListNode deleteDuplicates2(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while(cur!=null) {
            if(pre!= null && pre.val == cur.val) {
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
    //递归
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        if(head.val == head.next.val) return deleteDuplicates(head.next);
        head.next = deleteDuplicates(head.next);
        return head;
    }

    public static void main(String[] args) {
        Pro0083 pro0083 = new Pro0083();
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        head.next = node1;
        node1.next = node2;
        pro0083.deleteDuplicates(head);
    }
}
